<template>
  <div class="app-container">
    <el-card class="box-card mb20">
      <div class="statistics-container">
        <div class="stat-item">
          <span class="label">反馈总数</span>
          <span class="value">{{ stats.total || 0 }}</span>
        </div>
        <el-divider direction="vertical"></el-divider>
        <div class="stat-item">
          <span class="label">未处理</span>
          <span class="value warning-text">{{ stats.pendingCount || 0 }}</span>
        </div>
        <el-divider direction="vertical"></el-divider>
        <div class="stat-item">
          <span class="label">处理中</span>
          <span class="value info-text">{{ stats.processingCount || 0 }}</span>
        </div>
        <el-divider direction="vertical"></el-divider>
        <div class="stat-item">
          <span class="label">已处理</span>
          <span class="value success-text">{{ stats.completedCount || 0 }}</span>
        </div>
        <el-divider direction="vertical"></el-divider>
        <div class="stat-item">
          <span class="label">反馈类型</span>
          <span class="type-distribution">
            <el-tag size="mini" type="primary">问题:{{ stats.problemCount || 0 }}</el-tag>
            <el-tag size="mini" type="success">建议:{{ stats.suggestionCount || 0 }}</el-tag>
            <el-tag size="mini" type="info">其他:{{ stats.otherCount || 0 }}</el-tag>
          </span>
        </div>
      </div>
    </el-card>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="类型" clearable>
          <el-option label="问题反馈" value="问题反馈"/>
          <el-option label="功能建议" value="功能建议"/>
          <el-option label="其他反馈" value="其他反馈"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="反馈状态" clearable>
          <el-option label="未处理" :value="0"/>
          <el-option label="处理中" :value="1"/>
          <el-option label="已处理" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:feedback:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="反馈编号" align="center" prop="id" width="100"/>
      <el-table-column label="类型" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.type === '问题反馈' ? 'primary' : scope.row.type === '功能建议' ? 'success' : 'info'" size="medium">
            {{ scope.row.type }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="反馈内容" align="left" prop="content" :show-overflow-tooltip="true" min-width="250">
        <template slot-scope="scope">
          <div class="content-preview">
            <span class="text">{{ scope.row.content }}</span>
            <span v-if="scope.row.images" class="has-image">
              <i class="el-icon-picture-outline"></i>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="contact" width="150"/>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <div class="status-column">
            <el-tag :type="scope.row.status === 0 ? 'info' : scope.row.status === 1 ? 'warning' : 'success'" size="medium">
              {{ scope.row.status === 0 ? '未处理' : scope.row.status === 1 ? '处理中' : '已处理' }}
            </el-tag>
            <el-tooltip v-if="scope.row.reply" content="已回复" placement="top">
              <i class="el-icon-chat-dot-square reply-icon"></i>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['system:feedback:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-chat-dot-round" @click="handleReply(scope.row)"
            v-hasPermi="['system:feedback:reply']">回复</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 查看反馈详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body custom-class="view-dialog">
      <el-descriptions class="feedback-info" :column="2" border>
        <el-descriptions-item label="反馈类型">
          <el-tag size="medium">{{ form.type }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="处理状态">
          <el-tag :type="form.status === 0 ? 'info' : form.status === 1 ? 'warning' : 'success'" size="medium">
            {{ form.status === 0 ? '未处理' : form.status === 1 ? '处理中' : '已处理' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="联系方式">{{ form.contact || '无' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(form.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="反馈内容" :span="2">
          <div class="content-box">{{ form.content }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="附件" :span="2" v-if="form.images">
          <div class="image-grid">
            <div v-for="(url, index) in imageList" :key="index" class="image-item">
              <img :src="url" @click="previewImage(url)">
            </div>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="回复内容" :span="2" v-if="form.reply">
          <div class="reply-box">
            <div class="reply-time">回复时间：{{ parseTime(form.replyTime) }}</div>
            <div class="reply-content">{{ form.reply }}</div>
          </div>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 回复反馈对话框 -->
    <el-dialog title="回复反馈" :visible.sync="reply.open" width="800px" append-to-body custom-class="reply-dialog">
      <el-descriptions class="feedback-info" :column="2" border>
        <el-descriptions-item label="反馈类型">
          <el-tag size="medium">{{ reply.form.type }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="联系方式">{{ reply.form.contact || '无' }}</el-descriptions-item>
        <el-descriptions-item label="反馈内容" :span="2">
          <div class="content-box">{{ reply.form.content }}</div>
        </el-descriptions-item>
      </el-descriptions>
      
      <el-form ref="replyForm" :model="reply.form" :rules="reply.rules" label-width="100px" class="reply-form">
        <el-form-item label="回复内容" prop="reply">
          <el-input v-model="reply.form.reply" type="textarea" :rows="4" placeholder="请输入回复内容"/>
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="reply.form.status" placeholder="请选择处理状态">
            <el-option label="未处理" :value="0"/>
            <el-option label="处理中" :value="1"/>
            <el-option label="已处理" :value="2"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReplyForm">确 定</el-button>
        <el-button @click="reply.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 图片预览 -->
    <el-dialog :visible.sync="preview.open" width="800px" append-to-body class="image-preview-dialog">
      <img :src="preview.url" class="preview-image" alt="预览图片">
    </el-dialog>
  </div>
</template>

<script>
import {
  listFeedback,
  getFeedback,
  delFeedback,
  replyFeedback,
  getFeedbackStats,
  updateFeedback
} from "@/api/system/feedback";

export default {
  name: "Feedback",
  dicts: ['feedback_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 反馈表格数据
      feedbackList: null,
      // 统计数据
      stats: {
        total: 0,
        pendingCount: 0,
        processingCount: 0,
        completedCount: 0,
        problemCount: 0,
        suggestionCount: 0,
        otherCount: 0
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 回复参数
      reply: {
        open: false,
        form: {},
        rules: {
          reply: [
            { required: true, message: "回复内容不能为空", trigger: "blur" }
          ],
          status: [
            { required: true, message: "处理状态不能为空", trigger: "change" }
          ]
        }
      },
      // 图片预览
      preview: {
        open: false,
        url: ''
      }
    };
  },
  computed: {
    imageList() {
      return this.form.images ? this.form.images.split(',').filter(url => url) : [];
    }
  },
  created() {
    this.getList();
    this.getStats();
  },
  methods: {
    /** 查询反馈列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询统计数据 */
    getStats() {
      this.loading = true;
      getFeedbackStats().then(response => {
        this.stats = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        content: undefined,
        contact: undefined,
        images: undefined,
        reply: undefined,
        status: undefined
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      getFeedback(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看反馈";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除反馈编号为"' + ids + '"的数据项？').then(function() {
        return delFeedback(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 回复按钮操作 */
    handleReply(row) {
      this.reply.form = {
        id: row.id,
        type: row.type,
        content: row.content,
        contact: row.contact,
        reply: row.reply,
        status: row.status
      };
      this.reply.open = true;
    },
    /** 提交回复表单 */
    submitReplyForm() {
      this.$refs["replyForm"].validate(valid => {
        if (valid) {
          const replyData = {
            id: this.reply.form.id,
            reply: this.reply.form.reply
          };
          replyFeedback(replyData).then(response => {
            this.$modal.msgSuccess("回复成功");
            this.reply.open = false;
            this.getList();
          });
          // 更新状态
          if (this.reply.form.status !== undefined) {
            updateFeedback({
              id: this.reply.form.id,
              status: this.reply.form.status
            }).then(() => {
              this.getList();
            });
          }
        }
      });
    },
    /** 预览图片 */
    previewImage(url) {
      this.preview.url = url;
      this.preview.open = true;
    }
  }
};
</script>

<style scoped>
.mb20 {
  margin-bottom: 20px;
}

.statistics-container {
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 10px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 20px;
}

.stat-item .label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.stat-item .value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.warning-text {
  color: #E6A23C;
}

.info-text {
  color: #409EFF;
}

.success-text {
  color: #67C23A;
}

.type-distribution {
  display: flex;
  gap: 8px;
}

.content-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
}

.content-preview .text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.content-preview .has-image {
  color: #409EFF;
  font-size: 16px;
}

.status-column {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.reply-icon {
  color: #67C23A;
  font-size: 16px;
}

.view-dialog .el-dialog__body,
.reply-dialog .el-dialog__body {
  padding: 20px;
}

.feedback-info {
  margin-bottom: 20px;
}

.content-box {
  padding: 15px;
  background: #f8f9fa;
  border-radius: 4px;
  line-height: 1.6;
  white-space: pre-wrap;
  min-height: 100px;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 15px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 4px;
}

.image-item {
  position: relative;
  padding-top: 100%;
  overflow: hidden;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid #EBEEF5;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.image-item img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.image-item:hover img {
  transform: scale(1.05);
}

.reply-box {
  padding: 15px;
  background: #f8f9fa;
  border-radius: 4px;
  margin-top: 10px;
}

.reply-time {
  color: #909399;
  font-size: 13px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
}

.reply-time i {
  margin-right: 5px;
}

.reply-content {
  line-height: 1.6;
  white-space: pre-wrap;
  color: #303133;
}

.reply-form {
  margin-top: 20px;
  background: #f8f9fa;
  padding: 20px;
  border-radius: 4px;
}

.image-preview-dialog .el-dialog__body {
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #000;
}

.preview-image {
  max-width: 100%;
  max-height: calc(100vh - 100px);
  object-fit: contain;
}

.el-descriptions {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
}

.el-descriptions__label {
  font-weight: bold;
}

.no-files {
  color: #909399;
  text-align: center;
  padding: 30px;
  background: #f8f9fa;
  border-radius: 4px;
  font-size: 14px;
}
</style>
